const { Service } = require("egg");

class AwardListService extends Service {
  async create(data) {
    const { ctx } = this;
    return await ctx.model.AwardList.create(data).then((res) => {
      return res ? res.toJSON() : res;
    });
  }

  async findOne(where) {
    const { ctx } = this;
    return await ctx.model.AwardList.findOne({ where: where }).then((res) => {
      return res ? res.toJSON() : res;
    });
  }

  async list(kindId) {
    const { ctx } = this;

    const datas = await ctx.model.AwardList.findAll({
      raw: true,
      order: [["id", "ASC"]], // 根据 id 排序
    });

    if (kindId) {
      return datas.filter((item) => {
        return item.kindIds && Array.isArray(item.kindIds) && item.kindIds.includes(kindId);
      });
    } else {
      return datas;
    }
  }

  async update(where, updates) {
    const { ctx } = this;
    return await ctx.model.AwardList.update(updates, {
      where,
    });
  }

  async delete(where) {
    const { ctx } = this;
    return await ctx.model.AwardList.destroy({
      where,
    });
  }
}

module.exports = AwardListService;
